public class HomeActivity extends Activity implements View.OnClickListener{

    private ViewPager vpager;
    private MyPagerAdapter mAdapter;
    private ArrayList<Fragment> mFragments = new ArrayList<>();
    private RelativeLayout thirdLayout,firsrLayout,fourthLayout,secondLayout,myhome_serch,myhome_more;
    private ImageView thirdImage,fourthImage,firstImage,secondImage;
    private TextView firstText,secondText,thirdText,forthText;
    private Dialog inviteDialog;
    private LinearLayout addFriend,addGroup,addsys,addsfk;
    private int TextColor_Green=0xff46C11B,TextColor_Gray=0xff000000;
    private TextView unreadMsg;
    private View unreadMsg_pyq;
    private RelativeLayout RL; 
    private TextView TEXT;
    private static int nowPage=0;
    private ConversationFragment conversationFragment;
    private ContactFragment contactFragment;
    private FindFragment findFragment;
    private SettingFragment settingFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_myhome);
		//状态栏颜色
        StatusBarUtil.setTranslucentStatus(this);
        StatusBarUtil.setStatusBarDarkTheme(this,true);
        initView();
        nowPage=0;
        setChioceItem(0,false);
    }


    @Override
    public void onResume(){
        super.onResume();
        if(RefeshUtil.CAN_REFESH_HOME){
           if(RefeshUtil.CAN_REFESH_HOME_S1){
               if(conversationFragment!=null){
                   conversationFragment.refresh();
               }
           }
           if(RefeshUtil.CAN_REFESH_HOME_S2){
               if(findFragment!=null){
                   findFragment.readAllPyq();
                   unreadMsg_pyq.setVisibility(View.GONE);
               }
           }
        }
    }

    private void initView() {
        vpager = (ViewPager) findViewById(R.id.home_view_pager);
        thirdImage = (ImageView) findViewById(R.id.third_image);
        fourthImage = (ImageView) findViewById(R.id.forth_image);
        firstImage = (ImageView) findViewById(R.id.first_image);
        secondImage = (ImageView) findViewById(R.id.second_image);
        unreadMsg=(TextView)findViewById(R.id.unreadMsg);
        thirdLayout = (RelativeLayout) findViewById(R.id.third_layout);
        fourthLayout = (RelativeLayout) findViewById(R.id.forth_layout);
        firsrLayout = (RelativeLayout) findViewById(R.id.first_layout);
        secondLayout = (RelativeLayout) findViewById(R.id.second_layout);
        unreadMsg_pyq=(View)findViewById(R.id.unreadMsg_pyq);
        RL=(RelativeLayout)findViewById(R.id.wx_7_top_rl);
        TEXT=(TextView)findViewById(R.id.wx_7_top_text);
        firstText=(TextView)findViewById(R.id.title1);
        secondText=(TextView)findViewById(R.id.title2);
        thirdText=(TextView)findViewById(R.id.title3);
        forthText=(TextView)findViewById(R.id.title4);
        myhome_serch=(RelativeLayout)findViewById(R.id.myhome_serch);
        myhome_more=(RelativeLayout)findViewById(R.id.myhome_more) ;
        thirdLayout.setOnClickListener(HomeActivity.this);
        fourthLayout.setOnClickListener(HomeActivity.this);
        firsrLayout.setOnClickListener(HomeActivity.this);
        secondLayout.setOnClickListener(HomeActivity.this);
        myhome_serch.setOnClickListener(HomeActivity.this);
        myhome_more.setOnClickListener(HomeActivity.this);

        conversationFragment=new ConversationFragment();
        contactFragment=new ContactFragment();
        findFragment=new FindFragment();
        settingFragment=new SettingFragment();

        mFragments.add( conversationFragment);
        mFragments.add( contactFragment);
        mFragments.add(findFragment);
        mFragments.add(settingFragment);
        mAdapter = new MyPagerAdapter(getSupportFragmentManager());
        vpager.setAdapter(mAdapter);

        prepareMoveDialog();

        vpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            //viewpager滑动时，隐藏/显示fg2中的控件
            //position :当前页面，及你点击滑动的页面
            //positionOffset:当前页面偏移的百分比
            //positionOffsetPixels:当前页面偏移的像素位置
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                switch (positionOffsetPixels){
                    case 0:
                        if(contactFragment!=null){
                            contactFragment.showMy();
                        }
                        break;
                    default:
                        if(contactFragment!=null){
                            contactFragment.hideMy();
                        }
                        break;
                }
                if(nowPage==2&&positionOffsetPixels>10&&positionOffsetPixels<100){
                    RL.setVisibility(View.INVISIBLE);
                }
            }
            @Override
            public void onPageSelected(int position) {
                // position 当前选中的页面的Position
                setChioceItem(position,true);
            }
            @Override
            public void onPageScrollStateChanged(int state) {
                //arg0 ==1 正在滑动，arg0==2 滑动完毕，arg0==0 什么都没做。
                if(state==0){
                    if(nowPage==3){
                        RL.setVisibility(View.INVISIBLE);
                    }else {
                        RL.setVisibility(View.VISIBLE);
                    }
                }
                if(contactFragment!=null){
                    if(state==0){
                        contactFragment.showMy();
                    }else{
                        contactFragment.hideMy();
                    }
                }
            }
        });
        //开启service中的方法，用于数据统计
        //这里先省去此步骤，正式工程会加入
        //beginBackgroundWorks();
    }

    //重写-结果返回
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode,resultCode,data);
    }

    //响应接口-登出事件
    public void logout(){
        TlsBusiness.logout(UserInfo.getInstance().getId());
        UserInfo.getInstance().setId(null);
        MessageEvent.getInstance().clear();
        FriendshipInfo.getInstance().clear();
        GroupInfo.getInstance().clear();
        Intent intent = new Intent(HomeActivity.this,SplashActivity.class);
        finish();
        startActivity(intent);
    }

    //响应接口-新消息红点事件
    public void setMsgUnread(boolean show,String num){
        if(show){
            unreadMsg.setText(num);
            unreadMsg.setVisibility(View.VISIBLE);
        }else {
            unreadMsg.setVisibility(View.GONE);
        }
    }

    //重写-back
    @Override
    public void onBackPressed() {
        //super.onBackPressed();
        Intent i= new Intent(Intent.ACTION_MAIN);
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        i.addCategory(Intent.CATEGORY_HOME);
        startActivity(i);
    }

    //自定义-adapter
    private  class MyPagerAdapter extends FragmentPagerAdapter {
        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }
        @Override
        public int getCount() {
            return mFragments.size();
        }
        @Override
        public Fragment getItem(int position) {
            return mFragments.get(position);
        }
    }

    //重写-点击函数
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.first_layout:
                setChioceItem(0,false);
                RL.setVisibility(View.VISIBLE);
                break;
            case R.id.second_layout:
                setChioceItem(1,false);
                RL.setVisibility(View.VISIBLE);
                break;
            case R.id.third_layout:
                setChioceItem(2,false);
                RL.setVisibility(View.VISIBLE);
                break;
            case R.id.forth_layout:
                setChioceItem(3,false);
                RL.setVisibility(View.INVISIBLE);
                break;
            case R.id.myhome_serch:
                Intent intent = new Intent(HomeActivity.this, SearchFriendActivity.class);
                startActivity(intent);
                break;
            case R.id.myhome_more:
                inviteDialog.show();
                break;
            default:
                break;
        }
    }

    //设置-viewpager选择事件
    private void setChioceItem(int index,boolean onlyShow) {
        clearChioce();
        vpager.setCurrentItem(index,false);
        switch (index) {
            case 0:
                nowPage=0;
                TEXT.setText("微信");
                firstImage.setImageDrawable(getResources().getDrawable(R.drawable.wh_shouye_02));
                firstText.setTextColor(TextColor_Green);
                break;
            case 1:
                nowPage=1;
                TEXT.setText("通讯录");
                secondImage.setImageDrawable(getResources().getDrawable(R.drawable.wh_jiekuan_02));
                secondText.setTextColor(TextColor_Green);
                break;
            case 2:
                nowPage=2;
                TEXT.setText("发现");
                thirdImage.setImageDrawable(getResources().getDrawable(R.drawable.wh_huankuan_02));
                thirdText.setTextColor(TextColor_Green);
                break;
            case 3:
                nowPage=3;
                fourthImage.setImageDrawable(getResources().getDrawable(R.drawable.wh_wode_02));
                forthText.setTextColor(TextColor_Green);
                break;
        }
    }

    //方法-清除所有颜色配置
    private void clearChioce() {
        firstImage.setImageDrawable(getResources().getDrawable(R.drawable.wh_shouye_01));
        secondImage.setImageDrawable(getResources().getDrawable(R.drawable.wh_jiekuan_01));
        thirdImage.setImageDrawable(getResources().getDrawable(R.drawable.wh_huankuan_01));
        fourthImage.setImageDrawable(getResources().getDrawable(R.drawable.wh_wode_01));
        firstText.setTextColor(TextColor_Gray);
        secondText.setTextColor(TextColor_Gray);
        thirdText.setTextColor(TextColor_Gray);
        forthText.setTextColor(TextColor_Gray);
    }

    //方法-准备弹出框
    private void prepareMoveDialog() {
        inviteDialog = new Dialog(this, R.style.dialog);
        inviteDialog.setContentView(R.layout.contact_more);
        addFriend = (LinearLayout) inviteDialog.findViewById(R.id.pop_222);
        addGroup = (LinearLayout) inviteDialog.findViewById(R.id.pop_111);
        addsys=(LinearLayout)inviteDialog.findViewById(R.id.pop_333);
        addsfk = (LinearLayout) inviteDialog.findViewById(R.id.pop_444);
        addsfk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                inviteDialog.dismiss();
                Intent intent=new Intent(HomeActivity.this, Wx_sfk.class);
                startActivity(intent);

            }
        });
        addsys.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                inviteDialog.dismiss();
                tiaozhuan_sys();
            }
        });
        addFriend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                inviteDialog.dismiss();
                Intent intent = new Intent(HomeActivity.this, SearchFriendActivity.class);
                startActivity(intent);
            }
        });
        addGroup.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(HomeActivity.this, Wx_Mp.class);
                intent.putExtra("type","ql");
                startActivity(intent);
            }
        });
        getWindow().setBackgroundDrawableResource(android.R.color.transparent);
        Window window = inviteDialog.getWindow();
        window.setGravity(Gravity.TOP | Gravity.RIGHT);
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width=MyApplication.blackw;
        lp.height=MyApplication.blackh;
        window.setAttributes(lp);
    }


    private void tiaozhuan_sys(){
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
            if ( ContextCompat.checkSelfPermission(HomeActivity.this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED
                    ) {
                ActivityCompat.requestPermissions(HomeActivity.this, new String[]{Manifest.permission.CAMERA}, 1);
            } else {
                Intent intent = new Intent(HomeActivity.this, Wx_sys.class);
                startActivity(intent);
            }
        }else{
            Intent intent = new Intent(HomeActivity.this, Wx_sys.class);
            startActivity(intent);
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode){
            case 1:
            {
                if(grantResults.length ==1&&grantResults[0] == PackageManager.PERMISSION_GRANTED){
                    Intent intent = new Intent(HomeActivity.this, Wx_sys.class);
                    startActivity(intent); 
                } 
            }
            default:
                break;
        }
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }

    private void beginBackgroundWorks(){
        /*
          do something in background
        */
    }

}
